package com.tanhua.api.impl.mongo;

import com.tanhua.api.mongo.RecommendUserApi;
import com.tanhua.domain.mongo.RecommendUser;
import com.tanhua.domain.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service
public class RecommendUserApiImpl  implements RecommendUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public RecommendUser findBytodayBest(Long userid) {

        Query query = new Query(Criteria.where("toUserId").is(userid));
        query.with(new Sort(Sort.Direction.DESC,"score"));
        query.skip(0).limit(1); //只拿一条数据
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        return recommendUser; //最高缘分值 女神id
    }

    @Override
    public PageBeanVo findRecommendation(Integer page, Integer pagesize, Long userid) {

        Query query = new Query(Criteria.where("toUserId").is(userid));
        query.with(new Sort(Sort.Direction.DESC,"score"));
        query.skip( (page-1)*pagesize ).limit(pagesize);
        long count = mongoTemplate.count(query, RecommendUser.class);
        List<RecommendUser> list = mongoTemplate.find(query, RecommendUser.class);

        PageBeanVo pageBeanVo = new PageBeanVo(page,pagesize,count,list);

        return pageBeanVo;
    }

    @Override
    public RecommendUser findScore(Integer jrid, Long id) {
        Query query = new Query(Criteria.where("userId").is(jrid).and("toUserId").is(id));
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        return recommendUser;
    }
}
